package top.mypath.page;

import cn.hutool.core.util.ObjectUtil;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import org.apache.commons.collections4.CollectionUtils;

import top.mypath.model.dto.CacheData;
import top.mypath.util.ButtonBuilder;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LiYalin
 */
public class MainPage {

    private static Font labelFont = new Font("宋体", Font.PLAIN, 16);

    private static Font fieldFont = new Font("宋体", Font.PLAIN, 14);

    private static JFrame frame;

    public static JFrame getFrame() {
        return frame;
    }

    private CardLayout cardLayout;

    private JPanel centerPanel;

    private Integer showPage;

    private final static List<JPanel> showPanels = new ArrayList<>();

    public MainPage() {
        cardLayout = new CardLayout();
        showPage = 0;
    }

    public static Boolean addPage(int index, JPanel panel) {
        if (ObjectUtil.isEmpty(showPanels.get(index))) {
            showPanels.set(index, panel);
            return Boolean.TRUE;
        } else {
            throw new IllegalArgumentException("页面位置重复添加");
        }
    }

    public void start() throws NoSuchFieldException, IllegalAccessException {
        try {
            //    将窗体JFrame的 LookAndFeel 设定为 Windows
            UIManager.setLookAndFeel(new WindowsLookAndFeel());
        } catch (UnsupportedLookAndFeelException e1) {
            e1.printStackTrace();
        }
        frame = new JFrame("会计助手");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setSize(500, 350);
        frame.setLocationRelativeTo(null);
        createCenterPanel();
        JPanel southPanel = createSouthPanel();
        frame.add(centerPanel, BorderLayout.CENTER);
        frame.add(southPanel, BorderLayout.SOUTH);
        frame.setVisible(true);
    }

    private void createCenterPanel() throws NoSuchFieldException, IllegalAccessException {
        centerPanel = new JPanel(cardLayout);
        centerPanel.setBackground(new Color(255, 255, 255));
        centerPanel.setPreferredSize(new Dimension(460, 290));
        FirstPage firstPage = new FirstPage();
        SecondPage secondPage = new SecondPage();
        showPanels.add(firstPage);
        showPanels.add(secondPage);
        for (JPanel showPanel : showPanels) {
            centerPanel.add(showPanel);
        }
    }


    private JPanel createSouthPanel() {
        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setHgap(100);
        flowLayout.setVgap(18);
        JPanel southPanel = new JPanel(flowLayout);
        southPanel.setBackground(new Color(255, 250, 227));
        southPanel.setPreferredSize(new Dimension(460, 60));
        JButton beforeButton = ButtonBuilder.defaultButton("上一步");
        JButton afterButton = ButtonBuilder.defaultButton("下一步");
        JButton finishButton = ButtonBuilder.defaultButton("完成");
        beforeButton.setFont(labelFont);
        afterButton.setFont(labelFont);
        finishButton.setFont(labelFont);
        southPanel.add(afterButton);
        afterButton.addActionListener(e -> {
            FirstPage firstPage= (FirstPage) centerPanel.getComponent(0);
            firstPage.setLimitMoneyAndTaxRate();
            firstPage.setSplitManner();
            CacheData.cleaningData();
            CacheData.createRawCheckBoxItem();
            SecondPage secondPage = (SecondPage) centerPanel.getComponent(1);
            if(CollectionUtils.isNotEmpty(CacheData.getShowCheckBoxItems())){
                secondPage.flushedCheckBox();
            }
            southPanel.remove(afterButton);
            southPanel.add(beforeButton);
            southPanel.add(finishButton);
            southPanel.repaint();
            cardLayout.next(centerPanel);
            CacheData.dataConversion();
        });
        beforeButton.addActionListener(e -> {
            if(CollectionUtils.isNotEmpty(CacheData.getShowCheckBoxItems())) {
                CacheData.getShowCheckBoxItems().clear();
            }
            CacheData.getShowCheckBoxItemMap().clear();
            CacheData.getSelectedCheckBoxItems().clear();
            southPanel.remove(beforeButton);
            southPanel.remove(finishButton);
            southPanel.add(afterButton);
            southPanel.repaint();
            cardLayout.previous(centerPanel);
        });
        finishButton.addActionListener(e -> {
            SecondPage secondPage = (SecondPage) centerPanel.getComponent(1);
            secondPage.setOutputFile();
            if(CacheData.outputFileVerify()) {
                ProgressBarDialog progressBarDialog = new ProgressBarDialog(frame, "文件输出");
                progressBarDialog.setLocationRelativeTo(frame);
//                progressBarDialog.pack();
                progressBarDialog.setVisible(true);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        CacheData.outputFile(progressBarDialog);
                    }
                });

            }
        });
        return southPanel;
    }

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        MainPage mainPage = new MainPage();
        mainPage.start();
    }
}
